package com.qire.manhua.dal.service;

import android.graphics.Bitmap;
import android.view.View;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.StringUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.model.entity.UserEntity;
import com.qire.common.support.base.ProxyViewModel;
import com.qire.common.utils.FileManager;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.dal.IAppConfigDAL;
import com.qire.manhua.dal.ITaskDataDAL;
import com.qire.manhua.dal.IUserDataDAL;
import com.qire.manhua.model.vo.OkResponseVO;
import com.qire.manhua.model.vo.TaskListDataVO;
import com.qire.manhua.model.vo.TaskListDataVO.TaskDataItemVO;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class UserProfileEditService extends DataService {

    private final IUserDataDAL userDataDAL = WarehouseFactory.create(this, IUserDataDAL.class);
    private final ITaskDataDAL taskDataDAL = WarehouseFactory.create(this, ITaskDataDAL.class);
    private final IAppConfigDAL appConfigDAL = WarehouseFactory.create(this, IAppConfigDAL.class);

    private ProxyViewModel viewModel;

    public void onAddition(ProxyViewModel viewModel) {
        this.viewModel = viewModel;
        checkTask();
    }


    public void checkTask() {
        taskDataDAL.fetchTaskList();
    }
    @StrategyAnnotation(event = Url.TaskList)
    public void fetchTaskListApiCallbackRefresh(TaskListDataVO taskDataVO, boolean isCache){
        for(TaskDataItemVO vo : taskDataVO.aims_task){
            if(vo.task_id == 105){
                boolean isTaskUnfinished  = vo.state == 2;
                viewModel.set("rewardTipsVisible", isTaskUnfinished ? View.VISIBLE : View.GONE);
                break;
            }
        }
    }

    /**
     * 上传用户头像
     * @param userPortrait 用户头像
     */
    public void uploadUserPortrait(Bitmap userPortrait) {
        try {
            //新建文件存储裁剪后的图片
            File picture = FileManager.buildTempHeadPortraitFile();

            if(picture == null) {
                ToastHelper.showCenterDarkToast("", "创建图片文件失败，请检查权限和存储空间");
                return;
            }

            if(picture.exists()) {
                picture.delete();
            }

            //打开文件输出流
            FileOutputStream fos = new FileOutputStream(picture);
            //将bitmap压缩后写入输出流(参数依次为图片格式、图片质量和输出流)
            userPortrait.compress(Bitmap.CompressFormat.PNG, 85, fos);
            //刷新输出流
            fos.flush();
            //关闭输出流
            fos.close();

            appConfigDAL.uploadImage(picture);
        } catch (IOException e) {
            e.printStackTrace();
            return ;
        }
    }
    @StrategyAnnotation(event = Url.upload_img)
    public void uploadPortraitPictureApiCallBack(String dataUrlVO, boolean isCache){
        submitUserPortrait(dataUrlVO);
    }

    /**
     * 提交用户头像
     */
    public void submitUserPortrait(String userPortrait) {
        UserEntity.self.setPortrait(userPortrait);
        userDataDAL.submitUserPortrait(UserEntity.self.getPortrait());
    }

    /**
     * 提交用户名设置
     */
    public void submitUserName(String userName) {
        if(StringUtil.isEmpty(userName)) {
            return;
        }

        String oldName = UserEntity.self.getName();
        UserEntity.self.setName(userName);

        if(UserEntity.self.isIllegalName()) {
            UserEntity.self.setName(oldName);
            ToastHelper.showToast("请输入合法的昵称，昵称中不能包含非法字符，只能由数字、字母、中文组成");
            return;
        } else {
            userDataDAL.submitUserName(UserEntity.self.getName());
            viewModel.set("nicknameText", UserEntity.self.nicknameToText());
        }
    }

    /**
     * 提交性别设置
     * @param isMale true 是男性 false 是女性
     */
    public void submitSex(boolean isMale) {
        UserEntity.self.setSex(isMale);
        userDataDAL.submitUserSex(UserEntity.self.getSexValue());
        viewModel.set("genderText", UserEntity.self.sexToText());
    }

    /**
     * 提交生日设置
     */
    public void submitBirthday(String birthday) {
        UserEntity.self.setBirthday(birthday);
        userDataDAL.submitBirthday(UserEntity.self.getBirthday());
        viewModel.set("birthdayText", UserEntity.self.birthdayToText());
    }

    /**
     * 提交城市设置
     */
    public void submitCity() {
        userDataDAL.submitCity(UserEntity.self.getCity().index);
        viewModel.set("cityText", UserEntity.self.cityToText());
    }

    @StrategyAnnotation(event = Url.my_edit_info_ext)
    public void editUserProfileApiCallback(OkResponseVO dataVO, boolean isCache) {
        if(dataVO.isOK()) {
            ToastHelper.showSnackBar(dataVO.ok, Prompt.SUCCESS);
            viewModel.set("userPortrait", UserEntity.self.getPortrait());
            viewModel.set("nicknameText", UserEntity.self.nicknameToText());
            viewModel.set("genderText", UserEntity.self.sexToText());
            viewModel.set("birthdayText", UserEntity.self.birthdayToText());
            viewModel.set("cityText", UserEntity.self.cityToText());
        }
    }
}
